Laravel / Route / parameters & validations in route
Parameters in route
-
STEP
In route
# Defines a routes where id is optional parameter Route::get('/users/{id?}', 'UserController@get'); # Defines a routes where id is required parameter Route::get('/users/{id}', 'UserController@get'); in controller
in optional case
public function get( $id = null) { if (!$id) { // get id } } in mandatory case
public function get( $id) { //get id } validate route parameters
# Define a route where id should be numeric only Route::get('/users/{id}', 'UserController@get')->where('id', '[0-9]+'); # Define a route where name should be alpha only Route::get('/users/{name}', 'UserController@get')->where('name', '[A-Za-z]+'); # Define a route where name should be alpha only and id should be numeric only Route::get('/users/{id}/{name}', 'UserController@get')->where(['id' => '[0-9]+', 'name' => '[a-z]+']);